#include "serverSideOgrePlayer.h"
#include "../client/serverSideClient.h"


ServerSideOgrePlayer::ServerSideOgrePlayer(Ogre::SceneManager* mSceneMgr, ServerSideClient* client, std::string shapeName,int x, int y, int z) : ServerSidePlayer(client,shapeName,x,y,z)
{
	mClient = client;
}

ServerSideOgrePlayer::~ServerSideOgrePlayer()
{
}

	SceneNode* mBodyNode;
	Vector3 mKeyDirection;
	
	ServerSinbadCharacterController()
		//:mRoot(0),
		//mSceneMgr(0)
	{
		mRoot = new Ogre::Root("plugins_d.cfg");
		mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
		setupBody(mSceneMgr);
	}

	void ServerSideOgrePlayer::addTime(float deltaTime)
	{
		updateBody(deltaTime);
	}

	Vector3* ServerSideOgrePlayer::getKeyDirection()
	{
		return &mKeyDirection;
	}



void ServerSideOgrePlayer::setupBody(SceneManager* sceneMgr)
{
	// create main model
		mBodyNode = sceneMgr->getRootSceneNode()->createChildSceneNode(Vector3::UNIT_Y * CHAR_HEIGHT);
		
		mKeyDirection = Vector3::ZERO;
        mGoalDirection = Vector3::ZERO;

	}


void ServerSideOgrePlayer::updateBody(Real deltaTime)
	{
		mGoalDirection = Vector3::ZERO;   // we will calculate this
		Real yawAtSpeed;

		if (!mKeyDirection.isZeroLength())
		{
            mGoalDirection += mKeyDirection.z * Vector3::UNIT_Z;
			mGoalDirection += mKeyDirection.x * Vector3::UNIT_X;


			mGoalDirection.y = 0;
			mGoalDirection.normalise();

			Quaternion toGoal = mBodyNode->getOrientation().zAxis().getRotationTo(mGoalDirection,Vector3::UNIT_Y);

			// calculate how much the character has to turn to face goal direction
			Real yawToGoal = toGoal.getYaw().valueDegrees();

			// this is how much the character CAN turn this frame
			if(yawToGoal == 0.0)
               yawAtSpeed = 0.0;
			else
			   yawAtSpeed = yawToGoal / Math::Abs(yawToGoal) * deltaTime * TURN_SPEED;
	
			// turn as much as we can, but not more than we need to
			if (yawToGoal < 0) 
				yawToGoal = std::min<Real>(0, std::max<Real>(yawToGoal, yawAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, yawAtSpeed, 0);
			else if (yawToGoal > 0)
				yawToGoal = std::max<Real>(0, std::min<Real>(yawToGoal, yawAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, 0, yawAtSpeed);
			
			mBodyNode->yaw(Degree(yawToGoal));

			// move in current body direction (not the goal direction)
			mBodyNode->translate(0, 0, deltaTime * RUN_SPEED,
				Node::TS_LOCAL);

		}
	}

	Ogre::SceneManager* mSceneMgr;
	Ogre::Root *mRoot;
	Vector3 mGoalDirection;
	
}


void ServerSideOgrePlayer::processTick()
{
	float clientFrametime;

	float multiplier = 100.0f;

	clientFrametime = mClient->command.mMilliseconds / 1000.0f;;

	mClient->mPlayer->calculateVelocity(&mClient->command, clientFrametime);

	int f = mClient->mNetClient->GetIncomingSequence() & (COMMAND_HISTORY_SIZE-1);
	mClient->mProcessedFrame = f;

//brians code
	setKeyDirection();

    mClient->mPlayer->addTime(clientFrametime);
	
	mClient->command.mOrigin.x = mClient->mPlayer->mBodyNode->getPosition().x;
	mClient->command.mOrigin.z = mClient->mPlayer->mBodyNode->getPosition().z;

	if(mClient->command.mVelocity.x != 0.0 || mClient->command.mVelocity.z != 0.0)
	{
	   mClient->command.mVelocity.x = mClient->character->mBodyNode->getOrientation().zAxis().x;
	   mClient->command.mVelocity.z = mClient->character->mBodyNode->getOrientation().zAxis().z;
        }

	int f = mClient->mNetClient->GetIncomingSequence() & (COMMAND_HISTORY_SIZE-1);
	mClient->mProcessedFrame = f;

}
